<template>
    <div class="list" ref="wrapper">
        <div>
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="button-list">
                    <div class="button-wrapper">
                        <div class="button">{{currentCity}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="button-list">
                    <div class="button-wrapper" v-for="item in hotCities" :key="item.id" @click="handleCity(item.name)">
                        <div class="button">{{item.name}}</div>
                    </div>
                </div>
            </div>
            <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
                <div class="title border-topbottom">{{key}}</div>
                <div class="item-list">
                    <ul>
                        <li class="border-bottom" v-for="title in item" :key="title.id" @click="handleCity(title.name)">
                            {{title.name}}
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import Bscroll from 'better-scroll'
    import {mapState, mapMutations} from 'vuex'
    export default {
        props:{
            cities:Object,
            hotCities:Array,
            letter:String
        },
        data() {
            return {
                
            }
        },
        methods: {
            // 更改Vuex中的State
            handleCity(city){
                this.changeCity(city)
                this.$router.push('/')
            },
            // 把vuex中的Mutations映射到此处
            ...mapMutations(['changeCity'])
        },
        mounted() {
            this.scroll = new Bscroll(this.$refs.wrapper)
        },
        watch: {
            letter(){
                if(this.letter){
                    let el = this.$refs[this.letter][0]  //得到当前DOM中所引用的键（key）
                    this.scroll.scrollToElement(el,90)
                }
            }
        },
        computed: {
            // 把vuex中的city数据映射到该组件的computed当中，使用{{city}}即可得到数据
            ...mapState({ //可使用对象的形式
                currentCity:'city'
            })
        },
    }
</script>

<style lang="stylus" scoped>
@import '~@/assets/css/global.styl'
.border-topbottom
    &:before
        border-color #ccc
    &:after
        border-color #ccc
.border-bottom
    &:before
        border-color #ccc
.list
    position absolute
    top 1.58rem
    left 0
    right 0
    bottom 0
    overflow hidden
    .title
        line-height .54rem
        background #eee
        padding-left .2rem
        font-size .2rem
    .button-list
        padding .1rem .6rem .1rem .1rem
        overflow hidden
        .button-wrapper
            float left
            width 33.33%
            .button
                padding .1rem 0
                text-align center
                margin  .1rem
                border .02rem solid #ccc
                border-radius .06rem
    .item-list
        li
            line-height .76rem
            color #666
            padding-left .2rem
</style>